package com.liugui.genericsandcollections;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @ClassName: CollectionsDemo
 * @Author: liugui
 * @Date: 2020-11-04 14:43
 **/
public class CollectionsDemo {
    public static void main(String[] args) {
        List<Integer> list = new ArrayList<>();

        list.add(3);
        list.add(5);
        list.add(4);
        list.add(6);
        list.add(7);
        list.add(8);

        list.forEach(v-> System.out.print(v + "\t"));
        System.out.println();
        //随机排序
        Collections.shuffle(list);
        list.forEach(v -> System.out.print(v + "\t"));
        System.out.println();
        //反转
        Collections.reverse(list);
        list.forEach(v -> System.out.print(v + "\t"));
        System.out.println();
        //首尾交换
        Collections.swap(list,0,list.size()-1);
        list.forEach(v -> System.out.print(v + "\t"));
        System.out.println();
        //自然排序
        Collections.sort(list);
        list.forEach(v -> System.out.print(v + "\t"));
        //二分查找 必须排序后
        System.out.print("\n二分查找数值7的位置："+Collections.binarySearch(list, 7));
        //返回线程安全的list
        List<Integer> synchronizedList = Collections.synchronizedList(list);

    }

}
